package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import java.util.Objects;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowModelAppBusi;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowModelProcdef;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowProcessInstanceTraceVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowThirdPlatformUrl;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.app.FlowApp;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowAppRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowModelAppBusiRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowModelProcdefRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowProcessInstanceTraceRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowThirdPlatformUrlRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.ThirdPlatformCallbackResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2019-03-05 14:45
 */
@Service
public class DefaultThirdPlatformCallbackResolver implements ThirdPlatformCallbackResolver {

    @Autowired
    private FlowModelProcdefRepository flowModelProcdefRepository;

    @Autowired
    private FlowModelAppBusiRepository flowModelAppBusiRepository;

    @Autowired
    private FlowAppRepository flowAppRepository;

    @Autowired
    private FlowThirdPlatformUrlRepository flowThirdPlatformUrlRepository;

    @Autowired
    private FlowProcessInstanceTraceRepository flowProcessInstanceTraceRepository;

    @Override
    public String resolveCallbackUrl(String processDefId) {
        //1.根据流程定义id查询模型id
        FlowModelProcdef modelProcDef = flowModelProcdefRepository.getModelProcDef(processDefId);
        if (Objects.isNull(modelProcDef)) {
            //若记录不存在，则不处理
            return null;
        }

        return resolveUrlByModelId(modelProcDef.getModelId());
    }

    @Override
    public String resolveCallbackUrlByProcessInstanceId(String processInstanceId) {
        FlowProcessInstanceTraceVo processInstanceTrace = flowProcessInstanceTraceRepository.getProcessInstanceTraceByProcessInstanceId(processInstanceId);
        if (Objects.isNull(processInstanceTrace)) {
            return null;
        }
        return getCallbackUrlByAppId(processInstanceTrace.getAppId(), Type.CALL_BACK);

    }

    private String getCallbackUrlByAppId(Integer appId, Type callBack) {
        //3.根据应用id查询应用
        FlowApp appEntity = flowAppRepository.detail(appId);
        if (Objects.isNull(appEntity)) {
            return null;
        }

        return resolveUrl(appEntity.getAppCode(), callBack.getCode());
    }

    @Override
    public String resolveUrlByModelId(String modelId) {
        FlowModelAppBusi modelApp = flowModelAppBusiRepository.getAppByModelId(modelId);
        if (Objects.isNull(modelApp)) {
            return null;
        }

        return getCallbackUrlByAppId(modelApp.getAppId(), Type.CALL_BACK);

    }

    @Override
    public String resolveCandidateGroupUrl(Integer appId) {
        FlowApp appEntity = flowAppRepository.detail(appId);
        if (Objects.isNull(appEntity)) {
            return null;
        }
        return resolveUrl(appEntity.getAppCode(), Type.ROLE_QUERY.getCode());
    }

    @Override
    public String resolveUrl(String appCode, String type) {
        FlowThirdPlatformUrl flowThirdPlatformUrlInfo = flowThirdPlatformUrlRepository.get(appCode, type);
        if (Objects.nonNull(flowThirdPlatformUrlInfo)) {
            return flowThirdPlatformUrlInfo.getUrl();
        }
        return null;
    }
}
